//
//  BaseTabBarController.m
//  WindRunner
//
//  Created by leeqb on 9/9/14.
//  Copyright (c) 2014 poweropt. All rights reserved.
//

#import "BaseTabBarController.h"
#import "AppDelegate.h"
#import "EventViewController.h"
#import "ContactsViewController.h"

@interface BaseTabBarController ()
{
    NSInteger _prevSelectTag;
}
@end

@implementation BaseTabBarController

- (id)init
{
    self = [super init];
    if(self) {
        _prevSelectTag = 3;
        
        [self initControllers];
        [self initTabBars];
    }
    return self;
}

- (void)initControllers
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Message" bundle:nil];
    UINavigationController *navMessage = [sb instantiateInitialViewController];
    
    sb = [UIStoryboard storyboardWithName:@"Find" bundle:nil];
    UINavigationController *navFind = [sb instantiateInitialViewController];
    
    sb = [UIStoryboard storyboardWithName:@"Event" bundle:nil];
    UINavigationController *navEvent = [sb instantiateInitialViewController];
    
    sb = [UIStoryboard storyboardWithName:@"Contact" bundle:nil];
    UINavigationController *navContact = [sb instantiateInitialViewController];
    
    sb = [UIStoryboard storyboardWithName:@"Setting" bundle:nil];
    UINavigationController *navSetting = [sb instantiateInitialViewController];
    
    self.viewControllers = @[navMessage, navFind, navEvent, navContact, navSetting];
    self.selectedViewController = navEvent;
}

- (void)initTabBars
{
    UITabBar *tabBar = self.tabBar;
    tabBar.barStyle = UIBarStyleBlack;
    tabBar.barTintColor = [UIColor whiteColor];
    tabBar.selectedImageTintColor = RGB(30, 164, 235);
    tabBar.backgroundImage = [UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(64, 50)];
    tabBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor] size:CGSizeMake(64, 50)];
    
    CGRect rect;
    rect.origin.x = 0;
    rect.origin.y = 0;
    rect.size.width = SCREEN_WIDTH;
    rect.size.height = 5;
    UIView *shadowLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    shadowLine.backgroundColor = THEME_COLOR;
    shadowLine.layer.shadowColor = [UIColor lightGrayColor].CGColor;
    shadowLine.layer.shadowOpacity = 1;
    shadowLine.layer.shadowOffset = CGSizeMake(0, -1);
    [tabBar addSubview:shadowLine];
    
    UITabBarItem *tabMessage = [tabBar.items objectAtIndex:0];
    UITabBarItem *tabFind = [tabBar.items objectAtIndex:1];
    UITabBarItem *tabEvent = [tabBar.items objectAtIndex:2];
    UITabBarItem *tabContact = [tabBar.items objectAtIndex:3];
    UITabBarItem *tabSetting = [tabBar.items objectAtIndex:4];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    //[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:RGB(30, 164, 235), NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
    
    //[tabMessage setImageInsets:UIEdgeInsetsMake(5, 0, -5, 0)];
    tabMessage.tag = 1;
    tabMessage.image = [[UIImage imageNamed:@"bar_icon_message"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabMessage.selectedImage = [[UIImage imageNamed:@"bar_icon_message"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabMessage.title = LocalStringBase(@"TBAR_TITLE_MESSAGE");
    
    tabFind.tag = 2;
    tabFind.image = [[UIImage imageNamed:@"bar_icon_find"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabFind.selectedImage = [[UIImage imageNamed:@"bar_icon_find"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabFind.title = LocalStringBase(@"TBAR_TITLE_FIND");
    
    tabEvent.tag = 3;
    tabEvent.image = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabEvent.selectedImage = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [tabEvent setImageInsets:UIEdgeInsetsMake(-10, 0, 10, 0)];
    
    tabContact.tag = 4;
    tabContact.image = [[UIImage imageNamed:@"bar_icon_contact"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabContact.selectedImage = [[UIImage imageNamed:@"bar_icon_contact"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabContact.title = LocalStringBase(@"TBAR_TITLE_FRIEND");
    
    tabSetting.tag = 5;
    tabSetting.image =[[UIImage imageNamed:@"bar_icon_setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabSetting.selectedImage = [[UIImage imageNamed:@"bar_icon_setting"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    tabSetting.title = LocalStringBase(@"TBAR_TITLE_SETTING");
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(_prevSelectTag==item.tag && item.tag==3) {
        UINavigationController *navEvent = self.viewControllers[2];
        EventViewController *eventVC = (EventViewController *)navEvent.topViewController;
        AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        if(appDelegate.isSecret) {
            appDelegate.isSecret = NO;
            item.image = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [[UIImage imageNamed:@"home.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [item setImageInsets:UIEdgeInsetsMake(-10, 0, 10, 0)];
            [eventVC onSecretChange:NO];
        } else {
            appDelegate.isSecret = YES;
            item.image = [[UIImage imageNamed:@"bg_cloud.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            item.selectedImage = [[UIImage imageNamed:@"bg_cloud.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            [item setImageInsets:UIEdgeInsetsMake(6, 0, -6, 0)];
            [eventVC onSecretChange:YES];
        }
    }
    _prevSelectTag = item.tag;
}

- (void)setupUntreatedApplyCount
{
    NSInteger unreadCount = [[[ApplyViewController shareController] dataSource] count];
    UINavigationController *nav = self.viewControllers[3];
    if(nav) {
        if (unreadCount > 0) {
            nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%i",(int)unreadCount];
        }else{
            nav.tabBarItem.badgeValue = nil;
        }
    }
}

@end
